package own.application.test.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import own.application.test.entity.TestEntity;
import own.application.test.service.TestService;

@Controller
public class HelloController {

	@Autowired
	private TestService testService;

	@RequestMapping("/home")
	@ResponseBody
	String home() {
		
		return "Hello World!";
	}

	@RequestMapping("/login")
	String login() {
		return "login";
	}

	@RequestMapping(value = "/check_login", method = RequestMethod.POST)
	@ResponseBody
	String checkLogin(String userName, String password) {
		UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
		token.setRememberMe(true);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			if (subject.isAuthenticated()) {
				return "redirect:/home";
			}
		} catch (IncorrectCredentialsException e) {
			return "登录密码错误";
		} catch (ExcessiveAttemptsException e) {
			return "登录失败次数过多";
		} catch (LockedAccountException e) {
			return "帐号已被锁定";
		} catch (DisabledAccountException e) {
			return "帐号已被禁用";
		} catch (ExpiredCredentialsException e) {
			return "帐号已过期";
		} catch (UnknownAccountException e) {
			return "帐号不存在";
		} catch (UnauthorizedException e) {
			return "您没有得到相应的授权！";
		} catch (AccountException e) {
			return "您没有得到相应的授权！";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/view/login.html";
	}

	@RequestMapping("/context/inser_a")
	@ResponseBody
	String contextInserA() {
		return String.valueOf(testService.contextServiceA());
	}

	@RequestMapping("/context/inser_tx_a")
	@ResponseBody
	String contextInserTxA() {

		return String.valueOf(testService.contextServiceTxA());
	}

	@RequestMapping("/context/inser_test_entity")
	@ResponseBody
	String contextInserTestEnity() {
		TestEntity test = new TestEntity();
		test.setcId("5");
		test.setName("5");
		test.setTime("5");
		return String.valueOf(testService.contextInserTestEntity(test));
	}

	@RequestMapping("/context/redis/save")
	@ResponseBody
	String contextRedisSave() {
		testService.contextRedisSave("test", "test");
		return "1";
	}

	@RequestMapping("/context/redis/load")
	@ResponseBody
	String contextRedisLoad() {
		return testService.contextRedisLoad("test");
	}
	
	@RequestMapping("/elasticsearch/load")
	@ResponseBody
	String elasticSearchLoad() {
		return testService.ElasticSearchLoad();
	}
}
